package org.spring.security.oauth2.resource2.server;

import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Slf4j
public class PersonController {


    
    @GetMapping("/person")
    public String person() throws Exception{
        RestTemplate restTemplate = new RestTemplate();
        //header
        Map<String, String> requestHeaders = new HashMap<String, String>();
        requestHeaders.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        requestHeaders.put("Accept", MediaType.APPLICATION_JSON_VALUE);
        HttpHeaders headers = new HttpHeaders();
        headers.setAll(requestHeaders);
        String requestBody = "";
        HttpEntity<String> entity = new HttpEntity<String>(requestBody, headers);
        //url and parms
        String restUrl = "http://localhost:8088/oauth/token";
        //MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();
        //valueMap.put("grant_type", new ArrayList<String>().add("client_credentials"));
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(restUrl)          //.queryParams(valueMap);
                                                                .queryParam("grant_type", "client_credentials")
                                                                .queryParam("scope", "query")
                                                                .queryParam("client_id", "client_1")
                                                                .queryParam("client_secret", "123456");
        String url = uriComponentsBuilder.build().toUriString();
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        String responseMessage = response.getBody();
        log.info("responseMessage: " + responseMessage);
        ObjectMapper objectMapper = new ObjectMapper();
        AccessToken accessToken =   objectMapper.readValue(responseMessage,AccessToken.class);
        String access_token = accessToken.getAccess_token();
        log.info("access_token: " + access_token);

        //call api in resource 1
        restUrl = "http://localhost:7077/api/pet";
        uriComponentsBuilder = UriComponentsBuilder.fromUriString(restUrl).queryParam("access_token", access_token);
        url = uriComponentsBuilder.build().toUriString();
        response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        responseMessage = response.getBody();
        log.info("responseMessage: " + responseMessage);
        return "I'am a person";
    }


}
